package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
/**
 * @Title 支付宝支付回调记录
 * @author zhoutian   
 * @date 2015-11-26
 */
public class AliPayCallBackRecord {
	//商户系统唯一订单号
	private String orderNo;
	//通知时间
	private String notifyTime;
	//通知类型：异步通知、同步通知
	private String notifyType;
	//通知校验ID，用于验证该回调请求是该方发出
	private String notifyId;
	//签名方式,固定取值为RSA
	private String signType;
	//签名,用于验证回调请求参数是否被篡改过
	private String sign;
	//商品名称
	private String subject;
	//支付类型,默认值为：1（商品购买）
	private String paymentType;
	//支付宝交易号
	private String tradeNo;
	//交易状态码
	private String tradeStatus;
	//商户编号，商户（电桩）在第三方支付平台上开设的商户号码
	private String partnerNo;
	//卖家(商户)支付宝账号
	private String sellerEmail;
	//买家支付宝用户号
	private String buyerId;
	//买家支付宝账号，可以是Email或手机号码
	private String buyerEmail;
	//交易金额
	private String totalFee;
	//购买数量
	private String quantity;
	//商品单价,price等于total_fee
	private String price;
	//商品描述,该笔订单的备注、描述、明细等。对应请求时的body参数，原样通知回来
	private String orderDesc;
	//该笔交易创建的时间。格式为yyyy-MM-dd HH:mm:ss
	private String gmtCreate;
	//该笔交易的买家付款时间。格式为yyyy-MM-dd HH:mm:ss
	private String gmtPayment;
	//该交易是否调整过价格
	private String isTotalFeeAdjust;
	//买家是否在交易过程中使用了红包
	private String useCoupon;
	//折扣，支付宝系统会把discount的值加到交易金额上，如果有折扣，本参数为负数，否则为0.00,单位为元
	private String discount;
	//退款状态,REFUND_SUCCESS: 退款成功
	//全额退款情况：trade_status= TRADE_CLOSED，而refund_status=REFUND_SUCCESS；
	//非全额退款情况：trade_status= TRADE_SUCCESS，而refund_status=REFUND_SUCCESS
	//REFUND_CLOSED: 退款关闭
	private String refundStatus;
	//退款时间,卖家退款的时间，退款通知时会发送。格式为yyyy-MM-dd HH:mm:ss
	private String gmtRefund;
	//创建时间
	private Date createTime;
	
	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getNotifyTime() {
		return notifyTime;
	}

	public void setNotifyTime(String notifyTime) {
		this.notifyTime = notifyTime;
	}

	public String getNotifyType() {
		return notifyType;
	}

	public void setNotifyType(String notifyType) {
		this.notifyType = notifyType;
	}

	public String getNotifyId() {
		return notifyId;
	}

	public void setNotifyId(String notifyId) {
		this.notifyId = notifyId;
	}

	public String getSignType() {
		return signType;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getPaymentType() {
		return paymentType;
	}

	public void setPaymentType(String paymentType) {
		this.paymentType = paymentType;
	}

	public String getTradeNo() {
		return tradeNo;
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

	public String getTradeStatus() {
		return tradeStatus;
	}

	public void setTradeStatus(String tradeStatus) {
		this.tradeStatus = tradeStatus;
	}

	public String getPartnerNo() {
		return partnerNo;
	}

	public void setPartnerNo(String partnerNo) {
		this.partnerNo = partnerNo;
	}

	public String getSellerEmail() {
		return sellerEmail;
	}

	public void setSellerEmail(String sellerEmail) {
		this.sellerEmail = sellerEmail;
	}

	public String getBuyerId() {
		return buyerId;
	}

	public void setBuyerId(String buyerId) {
		this.buyerId = buyerId;
	}

	public String getBuyerEmail() {
		return buyerEmail;
	}

	public void setBuyerEmail(String buyerEmail) {
		this.buyerEmail = buyerEmail;
	}

	public String getTotalFee() {
		return totalFee;
	}

	public void setTotalFee(String totalFee) {
		this.totalFee = totalFee;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getOrderDesc() {
		return orderDesc;
	}

	public void setOrderDesc(String orderDesc) {
		this.orderDesc = orderDesc;
	}

	public String getGmtCreate() {
		return gmtCreate;
	}

	public void setGmtCreate(String gmtCreate) {
		this.gmtCreate = gmtCreate;
	}

	public String getGmtPayment() {
		return gmtPayment;
	}

	public void setGmtPayment(String gmtPayment) {
		this.gmtPayment = gmtPayment;
	}

	public String getIsTotalFeeAdjust() {
		return isTotalFeeAdjust;
	}

	public void setIsTotalFeeAdjust(String isTotalFeeAdjust) {
		this.isTotalFeeAdjust = isTotalFeeAdjust;
	}

	public String getUseCoupon() {
		return useCoupon;
	}

	public void setUseCoupon(String useCoupon) {
		this.useCoupon = useCoupon;
	}

	public String getDiscount() {
		return discount;
	}

	public void setDiscount(String discount) {
		this.discount = discount;
	}

	public String getRefundStatus() {
		return refundStatus;
	}

	public void setRefundStatus(String refundStatus) {
		this.refundStatus = refundStatus;
	}

	public String getGmtRefund() {
		return gmtRefund;
	}

	public void setGmtRefund(String gmtRefund) {
		this.gmtRefund = gmtRefund;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public AliPayCallBackRecord() {
		this.createTime = new Date();
	}
	
	public AliPayCallBackRecord(Map<String,String> params) {
		this.orderNo = params.get("out_trade_no");
		this.notifyTime = params.get("notify_time");
		this.notifyType = params.get("notify_type");
		this.notifyId = params.get("notify_id");
		this.signType = params.get("sign_type");
		this.sign = params.get("sign");
		this.subject = params.get("subject");
		this.paymentType = params.get("payment_type");
		this.tradeNo = params.get("trade_no");
		this.tradeStatus = params.get("trade_status");
		this.partnerNo = params.get("seller_id");
		this.sellerEmail = params.get("seller_email");
		this.buyerId = params.get("buyer_id");
		this.buyerEmail = params.get("buyer_email");
		this.totalFee = params.get("total_fee");
		this.quantity = params.get("quantity");
		this.price = params.get("price");
		this.orderDesc = params.get("body");
		this.gmtCreate = params.get("gmt_create");
		this.gmtPayment = params.get("gmt_payment");
		this.isTotalFeeAdjust = params.get("is_total_fee_adjust");
		this.useCoupon = params.get("use_coupon");
		this.discount = params.get("discount");
		this.refundStatus = params.get("refund_status");
		this.gmtRefund = params.get("gmt_refund");
		this.createTime = new Date();
	}
	
	private static final String select_all_sql = "select orderNo, notifyTime, notifyType, notifyId, signType, sign, subject, paymentType, tradeNo, tradeStatus, partnerNo, sellerEmail, buyerId, buyerEmail, totalFee, quantity, price, orderDesc, gmtCreate, gmtPayment, isTotalFeeAdjust, useCoupon, discount, refundStatus, gmtRefund, createTime from order_alipay_callback_record";
	
	public void save(){
		String sql = "insert into order_alipay_callback_record(orderNo, notifyTime, notifyType, notifyId, signType, sign, subject, paymentType, tradeNo, tradeStatus, partnerNo, sellerEmail, buyerId, buyerEmail, totalFee, quantity, price, orderDesc, gmtCreate, gmtPayment, isTotalFeeAdjust, useCoupon, discount, refundStatus, gmtRefund, createTime) values(:orderNo, :notifyTime, :notifyType, :notifyId, :signType, :sign, :subject, :paymentType, :tradeNo, :tradeStatus, :partnerNo, :sellerEmail, :buyerId, :buyerEmail, :totalFee, :quantity, :price, :orderDesc, :gmtCreate, :gmtPayment, :isTotalFeeAdjust, :useCoupon, :discount, :refundStatus, :gmtRefund, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update(){
		String sql = "update order_alipay_callback_record set orderNo = :orderNo, notifyTime = :notifyTime, notifyType = :notifyType, notifyId = :notifyId, signType = :signType, sign = :sign, subject = :subject, paymentType = :paymentType, tradeNo = :tradeNo, tradeStatus = :tradeStatus, partnerNo = :partnerNo, sellerEmail = :sellerEmail, buyerId = :buyerId, buyerEmail = :buyerEmail, totalFee = :totalFee, quantity = :quantity, price = :price, orderDesc = :orderDesc, gmtCreate = :gmtCreate, gmtPayment = :gmtPayment, isTotalFeeAdjust = :isTotalFeeAdjust, useCoupon = :useCoupon, discount = :discount, refundStatus = :refundStatus, gmtRefund = :gmtRefund, createTime = :createTime where orderNo = :orderNo";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public static AliPayCallBackRecord getBy(String orderNo){
		String sql = new StringBuilder(select_all_sql).append(" where orderNo = :orderNo").toString();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNo", orderNo);
		return BaseDAOUtil.queryForObject(sql, paramMap, AliPayCallBackRecord.class);
	}
	
	/**
	 * 根据多个订单号查询充电预约订单支付回调记录
	 * @param orderIds 订单号(多个)
	 * @return 充电预约订单支付回调记录
	 */
	public static List<AliPayCallBackRecord> getByIds(List<String> orderNos){
		String sql = new StringBuilder(select_all_sql).append(" where orderNo in (:orderNos)").toString();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNos", orderNos);
		return BaseDAOUtil.queryForList(sql, paramMap, AliPayCallBackRecord.class);
	}
}
